<?php
/**
 * Created by PhpStorm.
 * User: Apache
 *  展商  展品管理
 *
 */

namespace app\api\controller\basemanage;
use app\api\controller\common\ApiBase;
use think\Db;
use think\Model;
use app\api\model\Admin as AdminModel;
use app\api\controller\common\CommonPlus;
use think\Controller; // dengh quxiao
// Controller  ApiBase （dengl）
class Zscardattend extends ApiBase
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->admin = session('admin');
        //$this->model = model('CompanyPublic');
    }


    /**
     * 根据公海id获得获取
     * @return \think\response\Json|void
     *  http://dxshowxya.for.pub/index.php?s=/api/basemanage/Yuyuemanage/getList
     */

     public function getTabName($exhibition_id){
        $result=db('exhibition')->where(['id'=>$exhibition_id])->find();
        if($result){
            $tabName=$result['create_tablename'];
        }else{
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        return $tabName;
     }

    /**
     *
     * @return \think\response\Json|void
     *  comppub
     *
     */

    public function getList(){
        $page = $this->request->param('page');
        $page = max(1,intval($page));
        $pagesize =$this->request->param('pageSize')==""?20:$this->request->param('pageSize');
        $exhibition_id =$this->request->param('exhibition_id');
        if(empty($exhibition_id)){
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
         }
        $param=" and m.is_del=0";
        if(!empty($exhibition_id)){
            $param.=" and m.exhibition_id=$exhibition_id ";
        }
        $keyword =input('keyword');
        if (!empty($keyword)) {
            $param.=" and c.company_name like '%$keyword%' ";
        }

        $tabName =$this->getTabName($exhibition_id);
        $sql2="select m.*,e.title as exhibition_name,c.company_name as company_name_zs,c.id as company_id";
        $sql2.=" from crm_zscardattend m ";
        $sql2.=" left join crm_exhibition as e on e.id=$exhibition_id ";
        $sql2.=" left join crm_".$tabName." as c on c.id=m.id_zs";
        $sql2.=" where 1=1 $param";
        $sql2.=" order by m.id desc ";
        $sql2.=" limit ".($page-1)*$pagesize.",$pagesize ";
        $list=Db::query($sql2);
        $sql21=" select count(m.id) as count1 ";
        $sql21.=" from crm_zscardattend m ";
        $sql21.=" left join crm_exhibition as e on e.id=$exhibition_id ";
        $sql21.=" left join crm_".$tabName." as c on c.id=m.id_zs";
        $sql21.=" where 1=1 $param";
        $total1=Db::query($sql21);
        $total=$total1[0]['count1']; //st
        foreach ($list as &$v){
            $v['mobile']=$v['mobile']?substr_replace($v['mobile'], '****', 3, 4):'';
        }
        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'count'=>$total]]);
    }


    /**
     * 添加
     * @return \think\response\Json|void
     */
    public function add(){
        $com =new CommonPlus();
        $exhibition_id = $this->request->param('exhibition_id');
        $id_zs = $this->request->param('id_zs');
        if(empty($exhibition_id)){
           return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        if(empty($company_id)){
            //return json(['code'=>0,'msg'=>'请选择企业','data'=>'']);
        }
        // com
        $data['create_time']=time();
        $data['exhibition_id']=$exhibition_id;
        $data['id_zs']=$id_zs;
        //
        $data['name']=$this->request->param('name');   // 姓名
        $data['name_en']=$this->request->param('name_en');
        $data['mobile']=$this->request->param('mobile');
        $data['positions_cn']=$this->request->param('positions_cn'); // 职务
        $data['positions_en']=$this->request->param('positions_en');
        $data['company_name']=$this->request->param('company_name');
        $data['company_name_en']=$this->request->param('company_name_en'); //


        $res=$com->insert_com_dan($data, '', 'zscardattend');
        if(!$res){
            return json(['code'=>0,'msg'=>'操作失败','data'=>'']);
        }
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }

    /**
     * 删除
     */
    public function del(){
        $com =new CommonPlus();
        $id = $this->request->param('id');
        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误','data'=>'']);
        }
        $tabName='zscardattend';
        $r = db($tabName)->where(['id'=>$id])->find();
        if(!$r){
            return json(['code'=>0,'msg'=>'记录不存在','data'=>'']);
        }

        $where['id'] = $id;
        $data['is_del']=1;
        $r=$com->edit_com_fxlist($data, $where, $tabName);
        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试','data'=>'']);
        }
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }

    /**
     * 批量删除
     */
    public function delall(){
        $com =new CommonPlus();
        $tabName='zscardattend';
        $id = $this->request->param('id');
        $ids = explode(',',$id);
        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误','data'=>'']);
        }
        foreach ($ids as $id){
            $where['id'] = $id;
            $r=$com->sel_com_one('222', $where, $tabName);   

            if(!$r){
                return json(['code'=>0,'msg'=>'id:'.$id.'记录不存在,批量删除失败','data'=>'']);
            }else{
                $where1['id'] = $id;
                $data['is_del']=1;
                $r=$com->edit_com_fxlist($data, $where1, $tabName);
                if(!$r){
                    return json(['code'=>0,'msg'=>'id:'.$id.'删除失败','data'=>'']);
                }
            }
      
            
        }

        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
        
        Db::startTrans();
        try {
           // $map['id'] = array('in',$ids);
           // $r = $this->model->where($map)->delete(); 
           // Db::commit();
           // return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
        } catch (\Exception $e) {
            //如获取到异常信息，对所有表的删、改、写操作，都会回滚至操作前的状态：
           // Db::rollback();
          //  return json(['code'=>0,'msg'=>'操作失败，请重试','data'=>'']);
        }
       

    }



    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function edit()
    {
        $com =new CommonPlus();
        // com
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        //$create_uid  = $this->admin['id'];
        $data['update_time']=time();
       // $selfcontent = $this->request->param('selfcontent'); //自定义表单 理论上只有新增 才有 。。让前台隐藏吧

        $data['update_time']=time();
        // com
        $data['name']=$this->request->param('name');   // 姓名
        $data['name_en']=$this->request->param('name_en');
        $data['mobile']=$this->request->param('mobile');
        $data['positions_cn']=$this->request->param('positions_cn'); // 职务
        $data['positions_en']=$this->request->param('positions_en');
        $data['company_name']=$this->request->param('company_name');
        $data['company_name_en']=$this->request->param('company_name_en'); //


        $where['id'] = $id;
        $tbname="zscardattend";
        $r=$com->edit_com_fxlist($data, $where, $tbname);

        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功']);
    }
   // 明细
    public function getDetail()
    {
        $com =new CommonPlus();
        // com
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        $where['id'] = $id;
        $tbname="zscardattend";
        $r=$com->sel_com_one('222', $where, $tbname);
        //$r['question_content_sel']=unserialize($r['question_content_sel']);
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功','data'=>$r]);
    }

    // 查询下拉  获取所有列表 按照类别来
    public function selAll_xla_Bytype()
    {
        //==common
        $create_uid  = $this->admin['id'];
        // $id = $this->request->param('id');
        $type = $this->request->param('type');     //下拉属性

        //

        if (trim($type) == "gsxz") {
            // 公司性质
            $tbname = "xiala_gsxz"; // gtab

            $where = array('is_delete' => 0);
            // $where = "";
            $data = array('update_time' => time(), 'create_uid' => $create_uid);
        } elseif (trim($type) == "gsxt") {
            // 公司形态
            $tbname = "xiala_gsxt"; // gtab

            $where = array('is_delete' => 0);
            $data = array('update_time' => time(), 'create_uid' => $create_uid);
        }elseif (trim($type) == "gslevel") {
            // 公司级别
            $tbname = "xiala_gslevel";
            $where = array('is_delete' => 0);
            $data = array('update_time' => time(), 'create_uid' => $create_uid);
        }elseif (trim($type) == "sshy") {
            // 所属行业
            $tbname = "xiala_sshy";
            $where = array('is_delete' => 0);
            $data = array('update_time' => time(), 'create_uid' => $create_uid);
        }elseif (trim($type) == "ywxz") {
            // 业务性质
            $tbname = "xiala_ywxz";
            $where = array('is_delete' => 0);
            $data = array('update_time' => time(), 'create_uid' => $create_uid);
        }
        // var

        // ----------
        $list = model('Companyset')->selAll_common_xla($data, $where, $tbname);
        // var_dump ($r);die;
        if (!$list) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => 'success', 'data' => ['list' => $list]]);
        // return json(['code' => 1, 'msg' => '操作成功']);
    }




    // the end
    public function export()
    {
        $exhibition_id =$this->request->param('exhibition_id');
        $ids =$this->request->param('ids');
        if(empty($exhibition_id)){
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
         }
        $param=" and m.is_del=0";
        if(!empty($ids)){
            $param.=" and p.id in ($ids)";
        }else{
            if(!empty($exhibition_id)){
                $param.=" and m.exhibition_id=$exhibition_id ";
            }
            $keyword =input('keyword');
            if (!empty($keyword)) {
                $param.=" and c.company_name like '%$keyword%' ";
            }
        }

        $tabName =$this->getTabName($exhibition_id);
        $sql2="select m.*,h.title as hall_title,b.number as booth_number,concat(m.user_id,'·') as id_side,e.title as exhibition_name,c.company_name as company_name_zs,c.id as company_id,a.realname";
        $sql2.=" from crm_zscardattend m ";
        $sql2.=" left join crm_exhibition as e on e.id=$exhibition_id ";
        $sql2.=" left join crm_".$tabName." as c on c.id=m.id_zs";
        $sql2.=" left join crm_booth as b on b.agent_id=m.id_zs and b.exhibition_id=$exhibition_id ";
        $sql2.=" left join crm_admin as a on c.member_id=a.id";
        $sql2.=" left join crm_hall as h on h.id=b.hall_id";
        $sql2.=" where 1=1 $param";
        $sql2.=" order by m.id desc ";
        $list=Db::query($sql2);
        unset($v);
        $columns = array(
            array('title' => '中文名称', 'field' => 'name', 'width' => 12),
            array('title' => '英文名称', 'field' => 'name_en', 'width' => 12),
            array('title' => '身份证号码', 'field' => 'id_side', 'width' => 12),
            array('title' => '电话', 'field' => 'mobile', 'width' => 12),
            array('title' => '中文职务', 'field' => 'positions_cn', 'width' => 12),
            array('title' => '英文职务', 'field' => 'positions_en', 'width' => 12),
            array('title' => '中文公司', 'field' => 'company_name', 'width' => 12),
            array('title' => '英文公司', 'field' => 'company_name_en', 'width' => 12),
            array('title' => '所属展会', 'field' => 'exhibition_name', 'width' => 12),
            array('title' => '所属展商', 'field' => 'company_name_zs', 'width' => 12),
            array('title' => '展位号', 'field' => 'booth_number', 'width' => 12),
            array('title' => '展厅', 'field' => 'hall_title', 'width' => 12),
            array('title' => '业务员', 'field' => 'realname', 'width' => 12),
        );
        $title = "胸牌明细";
        $filename = $title . date('Y-m-d-H-i', time()) . ".xls";
        return json(['code'=>1,'msg'=>'操作成功','data'=>['columns'=>$columns,'filename'=>$filename,'list'=>$list]]);
    }


    /**
     * 根据公海id获得获取
     * @return \think\response\Json|void
     *  http://gwsp.linyuinfo.com//index.php?s=/api/basemanage/Zscardattend/wxSendTempinfo
     */

    public function wxSendTempinfo(){


        var_dump(111);die;
//        $result=db('exhibition')->where(['id'=>$exhibition_id])->find();
//        if($result){
//            $tabName=$result['create_tablename'];
//        }else{
//            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
//        }
//        return $tabName;
    }

}
